package dataextract;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2ExtractData {

	
	public static void main (String[] args)throws Exception{
		
		String path = "C:\\Users\\Alket\\codeindigo\\DBconnection\\CellacPerEvent2.txt";
		String inflow = "C:\\DATA\\data\\FinalData";
		
		List<Long> celllaclist = new ArrayList<Long>();
		loadData(path,celllaclist);
		
		Map<Long , List<Event>>map = new HashMap<Long, List<Event>>();
		
		List<Event> listcell = null;
		for(int j = 0; j < celllaclist.size(); j++) {
		 long celllac_j = celllaclist.get(j);
		 System.out.println("considero celllac "+celllac_j);
		 listcell = new ArrayList<Event>();
		 File Folder = new File(inflow);
		 File files[];
		 files = Folder.listFiles();
	
		 String filefullname = "";
		 try{
	        String line = "";

           if(files.length>1){
              for(int k = 0;k < files.length; k++){
            	  BufferedReader br = new BufferedReader(new FileReader(files[k]));
      			
      			String line2 ;
      			while((line2 = br.readLine())!= null){
      				String [] l = line2.split("\t");
      				String usr = l[0];
      				int imsi = Integer.parseInt(l[1]);
      				long celllac = Long.parseLong(l[2]);
      				long ts = Long.parseLong(l[3]);
      				
      				if(celllac_j == celllac){
      					listcell.add(new Event(usr, ts));
      					}
      	    	}br.close();
              }
           }
           
		 }catch(Exception e){
			e.getMessage();
		 }
         map.put(celllac_j, listcell);
         print(map, "EventsPerCellac_"+celllac_j+".txt");
		}
		
		
	}
	public static void loadData(String file, List<Long> list) throws Exception{
		
		 BufferedReader br = new BufferedReader(new FileReader(new File(file)));
			
			String line ;
			while((line = br.readLine())!= null){
				
				long celllac = Long.parseLong(line);
				list.add(celllac);
				
	    	}br.close();
		
		
	}
    public static void print(Map<Long, List<Event>>map , String file)throws Exception{
    	
    	PrintWriter out = new PrintWriter(new FileWriter(file));
    	for(long l: map.keySet()){
    		out.println(l);
    		
    		for (int i = 0; i < map.get(l).size(); i++) {
				out.print(map.get(l).get(i).usr+", ");
				out.println(map.get(l).get(i).ts);
			}
    	}
    	out.close();
    }

}
